import HttpRequest from './axios'
import config from '../config'
const baseUrl = process.env.NODE_ENV === 'development' ? config.baseUrl.dev : config.baseUrl.pro
const port = process.env.NODE_ENV === 'development' ? config.port.dev : config.port.pro
// get请求
export function get (url, param) {
    return new Promise((resolve, reject) => {
        const getRequest = new HttpRequest(baseUrl + port)
        getRequest.request({
            method: 'get',
            url,
            params: param
        }).then((res) => {
            resolve(res)
        }).catch((err) => {
            reject(err)
        })
    })
}
// post请求
export function post (url, param,headers) {
    return new Promise((resolve, reject) => {
        const postRequest = new HttpRequest(baseUrl + port)
        postRequest.request({
            method: 'post',
            url,
            data: param,
            headers: headers ? headers : {}
        }).then((res) => {
            resolve(res)
        }).catch((err) => {
            reject(err)
        })
    })
}
// post请求 文件上传
export function filePost (url, param,callback,headers) {
    return new Promise((resolve, reject) => {
        const postRequest = new HttpRequest(baseUrl + port)
        postRequest.request({
            method: 'post',
            url,
            data: param,
            headers: headers ? headers : {},
            onUploadProgress: function (progress) {
                if (progress.lengthComputable) {
                    callback(progress)
                }
            }
        }).then((res) => {
            resolve(res)
        }).catch((err) => {
            reject(err)
        })
    })
}
